<?php
// 实现页面的延迟跳转
function redirect($url, $msg)
{
	//用html方法实现页面延迟跳转
	/*echo "<html>\n";
	 echo "<head>\n";
	echo "<meta http-equiv=\"refresh\" content=\"5;url=".$url."\">\n";
	echo "</head>\n";
	echo "<body>\n";
	echo $msg."</br>\n";
	echo "页面将在5秒后自动跳转...</br>\n";
	echo "<a href=\"".$url."\">如果没有跳转，请点这里跳转</a>\n";
	echo "</body>\n";
	echo "</html>\n";*/

	//用js方法实现页面延迟跳转
	echo $msg."</br>\n";
	echo "页面将在3秒后自动跳转...</br>\n";
	echo "<a href=\"".$url."\">如果没有跳转，请点这里跳转</a>\n";
	echo "<script language=\"javascript\">setTimeout(\"window.location.href='".$url."'\",3000)
		 </script>";
}

// 获取文章内容
function retrieve_entries(PDO $dblink, $page="blog", $url=NULL) {
	// 如果提供了 ID 号，则获取一篇文章，否则获取所有文章的 ID 和标题
	if (isset($url)) {
		$sql = "SELECT id, page, title, image, entry, created FROM entries WHERE url=? LIMIT 1";
		$stmt = $dblink->prepare($sql);
		$tt = $stmt->execute(array($url));
		$data = $stmt->fetch();
		$data['url'] = $url;
		$full_disp = 1;
	} else {
		// 按照创建日期降序排列
		$sql = "SELECT id, page, title, image, entry, url, created FROM entries WHERE page=? ORDER BY created DESC";
		$stmt = $dblink->prepare($sql);
		$stmt->execute(array($page));
		$data = NULL;
		foreach ($stmt as $row) {
			if ($page == "about") {
				$data = array("id" 		=>  $row["id"],
							  "page" 	=>  $row["page"],
							  "title" 	=> 	$row["title"],
							  "image"	=> 	$row["image"],
							  "entry" 	=>	$row["entry"],
							  "url"		=>	$row["url"],
							"created"	=>	$row["created"]);
			} else {
				$data[] = array("id" 		=>  $row["id"],
								"page" 		=>  $row["page"],
								"title" 	=> 	$row["title"],
								"image"		=> 	$row["image"],
								"entry" 	=>	$row["entry"],
								"url"		=>	$row["url"],
								"created"	=>	$row["created"]);
			}
		}
		// 当前没有文章, 就给出一个默认输出
		if (!isset($data)) {
			$full_disp = 1;
			$data = array("title" => "目前没有文章",
						  "entry" => '<a href="/SimpleBlog/admin/'."$page".'">发布第一篇文章!</a>');
		} else {
			$full_disp = 0;
			if ($page == 'about') {
				$full_disp = 1;
			}
		}
	}
	return array($data, $full_disp);
}

// 清理将要显示的内容
function santinize_data($data) {
	if (!is_array($data)) {
		return strip_tags($data, "<b><a>");
	} else {
		return array_map("santinize_data", $data);
	}
}

// 将文章标题转换为搜索引擎友好的形式
function make_url($title) {
	// u 选项用于打开对 UTF-8 的支持
	$p = array(
		"/\s+/u",
		"/(?!-)\W+/u" # negtive lookahead: 接下来的字符不能是横杠, 若满足, 再匹配 \W
	);
	$r = array("-", "");
	return preg_replace($p, $r, strtolower($title));
}

// 生成内容管理链接
function admin_link($page, $url) {
	$url = urlencode(urlencode($url));
	$edit_link = "/SimpleBlog/admin/$page/$url";
	$delete_link = "/SimpleBlog/admin/delete/$url";
	$admin['edit'] = "<a href=\"$edit_link\">编辑文章</a>";
	$admin['delete'] = "<a href=\"$delete_link\">删除文章</a>";
	return $admin;
}

// 生成确认删除的表单
function confirm_delete($link, $url) {
	$e = retrieve_entries($link, "", $url);
	// 多重数组必须用复杂形式来解析字符串
	return <<<FORM
<form action="/SimpleBlog/admin.php" method="POST">
	<fieldset><legend>确认删除</legend>
		<p>确定要删除文章 "{$e[0]["title"]}"?</p>
		<input type="submit" name="submit" value="是" />
		<input type="submit" name="submit" value="否" />
		<input type="hidden" name="action" value="delete" />
		<input type="hidden" name="url" value="$url" />
	</fieldset>
</form>
FORM;
}

function delete_entry(PDO $link, $url) {
	$sql = "DELETE FROM entries WHERE url=? LIMIT 1";
	$stmt = $link->prepare($sql);
	return $stmt->execute(array($url));
}

function format_image($img_path, $alt) {
	if (!empty($img_path)) {
		return "<img src=\"$img_path\" alt=\"$alt\" />";
	} else {
		return NULL;
	}
}

function create_user_form() {
	return <<<FORM
<form action="/SimpleBlog/inc/update.inc.php" method="POST">
	<fieldset>
	<legend>注册新的管理员</legend>
	<label>用户名
	<input type="text" name="username" maxlength="75" />
	</label>
	<label>密码
	<input type="password" name="password" />
	</label>
		<input type="submit" name="submit" value="创建" />
		<input type="submit" name="submit" value="取消" />
		<input type="hidden" name="action" value="createuser" />
	</fieldset>
</form>
FORM;
}

?>